package cn.wolfcode.cms.web.controller;

import cn.wolfcode.cms.annotation.PermissionName;
import cn.wolfcode.cms.domain.NumCharge;
import cn.wolfcode.cms.query.QueryObject;
import cn.wolfcode.cms.service.INumChargeService;
import cn.wolfcode.cms.util.JSONResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

//冲次
@Controller
@RequestMapping("numCharge")
public class NumChargeController {

    @Autowired
    private INumChargeService numChargeService;

    @RequiresPermissions("numCharge:index")
    @PermissionName("冲次首页")
    @RequestMapping("")
    public String index() {
        numChargeService.list();
        return "numCharge/list";
    }

    //冲次信息列表/带分页
    @RequiresPermissions("numCharge:list")
    @PermissionName("冲次列表")
    @RequestMapping("list")
    @ResponseBody
    public Object list(QueryObject qo) {
        return numChargeService.query(qo);
    }


    //所有冲次信息数据/无分页
    @RequestMapping("listData")
    @ResponseBody
    public Object listData() {
        return numChargeService.list();
    }


    //冲次添加
    @RequiresPermissions("numCharge:add")
    @PermissionName("冲次添加")
    @RequestMapping("save")
    @ResponseBody
    public Object save(@RequestParam Map<String, String> entity) {
        JSONResult result = new JSONResult();
        try {
            numChargeService.save(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("添加失败");
        }
        return result;
    }

    //冲次编辑
    @RequiresPermissions("numCharge:edit")
    @PermissionName("冲次编辑")
    @RequestMapping("edit")
    @ResponseBody
    public Object edit(NumCharge entity) {
        JSONResult result = new JSONResult();
        try {
            numChargeService.update(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("编辑失败");
        }
        return result;
    }

    //冲次删除
    @RequiresPermissions("numCharge:delete")
    @PermissionName("冲次删除")
    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long id) {
        JSONResult result = new JSONResult();
        try {
            numChargeService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("删除失败");
        }
        return result;
    }

    @RequestMapping("getRecordBykeyInfo")
    @ResponseBody
    public Object getRecordBykeyInfo(String keyInfo) {
        return numChargeService.getRecordBykeyInfo(keyInfo);
    }
}
